package cn.zz.comm.types.result;

import com.fasterxml.jackson.annotation.JsonInclude;

import cn.zz.comm.exception.BizException;
import cn.zz.comm.exception.enums.SysErrorCode;
import lombok.Getter;
import lombok.Setter;

/**
 * @author zhongq
 * @since 2022/12/28
 */
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> {
	private String code;
	private String message;
	private T result;

	public R() {
	}

	public R(String code, String message, T data) {
		this.code = code;
		this.message = message;
		this.result = data;
	}

	public static <T> R<T> ok() {
		return ok(null);
	}

	public static <T> R<T> ok(T data) {
		SysErrorCode code = SysErrorCode.SUCCESS;
		return new R<>(code.code(), code.pattern(), data);
	}

	public static R<Object> fail(BizException e) {
		R<Object> r = new R<>();
		r.setCode(e.getCode());
		r.setMessage(e.getMessage());
		r.setResult(e.getData());
		return r;
	}
}